package com.woniuxy.config.pgsql;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.postgresql.jdbc.PgParameterMetaData;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingOptions;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Author: 马宇航
 * @Description: TODO
 * @DateTime: 25/10/31/星期五 14:32
 * @Component: 成都蜗牛学苑
 **/
@Configuration
public class PgSqlConfig {
    @Bean
    public DataSource pgDatasource(CumsumerPgProperties properties){
        // 1. 配置Hikari参数
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(properties.getUrl()); // 绑定自定义url
        hikariConfig.setDriverClassName(properties.getDriverClassName()); // 绑定自定义driver
        hikariConfig.setUsername(properties.getUsername());
        hikariConfig.setPassword(properties.getPassword());
        // 绑定连接池参数
        hikariConfig.setMaximumPoolSize(properties.getHikari().getMaximumPoolSize());
        hikariConfig.setConnectionTimeout(properties.getHikari().getConnectionTimeout());
        // 2. 创建Hikari数据源
        return new HikariDataSource(hikariConfig);
    }
    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("pgDatasource") DataSource pgDatasource) {
        // 可在此处自定义JdbcTemplate的配置（如查询超时时间等）
        JdbcTemplate jdbcTemplate = new JdbcTemplate(pgDatasource);
        // 示例：设置查询超时时间为30秒
        jdbcTemplate.setQueryTimeout(30);
        return jdbcTemplate;
    }
    @Bean
    public PgVectorStore vectorStore(JdbcTemplate jdbcTemplate, DashScopeEmbeddingModel model) {
        return PgVectorStore.builder(jdbcTemplate, model).build();
    }
}
